<?php

namespace My\Controller;

use Utuo\BackgroundController;

class FeedbackController extends BackgroundController {
	
	protected $status= array(
			'0' => "未审核",
			'1' => "审核通过",
			'2' => "审核未通过",
	);
	
	// 初始化
	public function _initialize() {
		$this->setControllerTitle('素材管理');
		$this->setActionTitle(array(
				'index' => '问题列表',
				'recycle' => '回收站',
				'reply' => '回复问题'
		));
		$this->setActionNav(array(
				'index',
				'recycle'
		));
	}
	
	public function indexAction() {
		$this->getLists( 0 );
	}
	
	public function recycleAction() {
		$this->getLists( 1 );
	}
	
	public function getLists($is_deleted) {
		$condition = array(
			'is_deleted' => $is_deleted,
			'cid' => $this->aid,
		);
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$feedbackLists = M('Feedback')->where($condition)->field('*')->limit($pagesize)->page($page)->order('id desc')->select();
		foreach ($feedbackLists as &$v) {
			$v['statusOptions'] = $this->getOption($this->status, $v['status'], null);
		}
		$count = M('Feedback')->where($condition)->count();
		$this->assign('feedbackLists', $feedbackLists);
		$this->showPage($count, $pagesize);
		$this->display();
	}	
	
	// 设置审核状态
	public function setVerifyStatusAction() {
		$id = I('id',0, 'intval');
		$status = I('status', 0, 'intval');
		false !== M("Feedback")->where(array('id'=>$id))->setField('status', $status) and $this->ajaxSuccess("设置成功 ");
		$this->ajaxSuccess("设置失败");
	}
	
	// 问题回复页
	public function replyAction() {
		$id = I('id',0,'intval');
		$feedback = M("Feedback")->where(array('id'=>$id, 'cid'=>$this->aid))->find();
		$this->assign("feedback", $feedback);
		$this->display();
	}
	
	// 回复保存
	public function saveAction() {
		$d = D("Feedback");
		if ($d->field("answer,id")->create()) {
			$d->reply_time = time();
			false!==$d->save() and $this->ajaxSuccess("回复成功!");
		}
		$this->ajaxError("回复失败");
	}
	
	// 恢复
	public function restoreAction() {
		$this->ajaxRestore("Feedback");
	}
	
	// 删除
	public function deleteAction() {
		$this->ajaxDelete("Feedback");
	}
	
	// 移除
	public function removeAction() {
		$this->ajaxRemove("Feedback");
	}
	
}